//
//  JWShopMeVC.m
//  jinwang
//
//  Created by jzb on 2018/3/23.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWShopMeVC.h"
#import "SettingsVC.h"
#import "JWShopMeCell.h"
#import "GeRenZiLiaoVC.h"
#import "JWAddressVC.h"
#import "JWShopMeMyOrderVC.h"
#import "JWShopMeMyCollectionVC.h"
#import "JWShopMeMyCouponVC.h"
#import "JWShopMeEvaluationVC.h"
#import "JWShopMeMerchantEntryVC.h"
#import "JWShopMeCompleteMerchantEntryVC.h"
@interface JWShopMeVC ()

@property (nonatomic,strong) UIButton *preserveBtn;
@property (nonatomic,strong) JWShopMeCell*cell;

@end

@implementation JWShopMeVC

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *preserveItem = [[UIBarButtonItem alloc]initWithCustomView:self.preserveBtn];
    self.navigationItem.rightBarButtonItem = preserveItem;
    
    [self setUpTableView];
    [self net_getUserInfo];
}
//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWShopMeCell class])]];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        BOOL isLogin = [[ToolManager sharedManager] checkLoginStateAtVC:self];
        if (isLogin) {
            [self net_getUserInfo];
        }
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[JWShopMeCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    
    [self.cell.HeaderImg sd_setImageWithURL:[NSURL URLWithString:k_user_headsmall] placeholderImage:[UIImage imageNamed:@"jw_img_404"]];
    
    if (kStringIsEmpty(k_user_phone)) {
        self.cell.User_PhoneLabel.text = @"账号：";
    }else{
        self.cell.User_PhoneLabel.text = [NSString stringWithFormat:@"账号：%@",k_user_phone];
    }
    
    if (kStringIsEmpty(k_user_nickname)) {
        self.cell.User_NicknameLabel.text = @"昵称：";
    }else{
        self.cell.User_NicknameLabel.text = [NSString stringWithFormat:@"昵称：%@",k_user_nickname];
    }
    
    [self.cell.myOrderBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.daifukuanBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.daifahuoBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.daishouhuoBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.daipingjiaBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.tuihuoBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.wodeshoucangBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.wodeyouhuiquanBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.wodepingjiaBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.kefuBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.addressBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.shopRuzhuBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    
    //单击的手势
    UITapGestureRecognizer *click = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerBGViewTap:)];
    
    //点击几次后触发事件响应，默认为：1
    click.numberOfTapsRequired = 1;
    //需要几个手指点击时触发事件，默认：1
    click.numberOfTouchesRequired = 1;
    
    self.cell.headerBGView.userInteractionEnabled = YES;
    
    [self.cell.headerBGView addGestureRecognizer:click];
    
    //单击的手势
    UITapGestureRecognizer *Labelclick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(User_NicknameLabelTap:)];
    
    //点击几次后触发事件响应，默认为：1
    Labelclick.numberOfTapsRequired = 1;
    //需要几个手指点击时触发事件，默认：1
    Labelclick.numberOfTouchesRequired = 1;
    
    self.cell.User_NicknameLabel.userInteractionEnabled = YES;
    
    [self.cell.User_NicknameLabel addGestureRecognizer:Labelclick];
    
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 650;
}
- (void)headerBGViewTap:(UITapGestureRecognizer *)gesture{
    
    GeRenZiLiaoVC *VC = [[GeRenZiLiaoVC alloc]init];
    
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)User_NicknameLabelTap:(UITapGestureRecognizer *)gesture{
    
    NSLog(@"111111");
}
- (void)cellButtonOnclikc:(UIButton *)sender{
    
    NSLog(@"-=-=-=-=-=>>%ld",(long)sender.tag);
    
    if (sender.tag == 1000) {
        //我的订单
        JWShopMeMyOrderVC *VC = [[JWShopMeMyOrderVC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];

    }else if (sender.tag == 1001){
       //待付款

    }else if (sender.tag == 1002){
       //待发货

    }else if (sender.tag == 1003){
        //待收货

    }else if (sender.tag == 1004){
        //待评价
        
    }else if (sender.tag == 1005){
        //退货
        
    }else if (sender.tag == 1006){
        //我的收藏
        JWShopMeMyCollectionVC *VC = [[JWShopMeMyCollectionVC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }else if (sender.tag == 1007){
        //我的优惠券
        JWShopMeMyCouponVC *VC = [[JWShopMeMyCouponVC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }else if (sender.tag == 1008){
        //我的评价
        JWShopMeEvaluationVC *VC = [[JWShopMeEvaluationVC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }else if (sender.tag == 1009){
        //联系客服
        
    }else if (sender.tag == 1010){
        //收货地址
        
        JWAddressVC *VC = [[JWAddressVC alloc]init];
        
        [self.navigationController pushViewController:VC animated:YES];
    }else if (sender.tag == 1011){
        //商家入驻
//        JWShopMeMerchantEntryVC *VC = [[JWShopMeMerchantEntryVC alloc]init];
        JWShopMeCompleteMerchantEntryVC *VC = [[JWShopMeCompleteMerchantEntryVC alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
}
#pragma mark -网络请求
//获取用户信息
-(void)net_getUserInfo{
    [[DDNetworkManagerDate makeUrl_getuserinfo_with_token:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        NSLog(@"----->>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            NSDictionary *data = [responseObject objectForKey:kRequsetDataKey];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_id"]] forKey:@"user_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_nickname"]] forKey:@"user_nickname"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_portrait"]] forKey:@"user_headsmall"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_money"]] forKey:@"user_money"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_truename"]] forKey:@"user_truename"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_birthday"]] forKey:@"user_birthday"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sex"]] forKey:@"user_sex"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"media_id"]] forKey:@"media_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"shop_id"]] forKey:@"shop_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sign"]] forKey:@"user_sign"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_qrcode"]] forKey:@"user_qrcode"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sn"]] forKey:@"user_sn"];
            
            [kUserDefaults synchronize];
        }else{
            //注销token
            [kUserDefaults removeObjectForKey:@"user_token"];
            [kUserDefaults synchronize];
        }
        
        //刷新
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
#pragma mark -懒加载
- (UIButton *)preserveBtn{
    if (!_preserveBtn) {
        _preserveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _preserveBtn.frame = CGRectMake(0, 0, 44, 44);
        [_preserveBtn setTitle:@"设置" forState:UIControlStateNormal];
        [_preserveBtn addTarget:self action:@selector(preserveButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _preserveBtn;
}

- (void)preserveButOnclick:(UIButton *)sender{
    
    SettingsVC *VC = [[SettingsVC alloc]init];
    
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
